/**
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 * @author Jaune
 * @version 1.0.0
 */
(function (global) {
    var Me = Me || {};

    global.Me = Me; //make Me as public

    Me.globalSetting = {};
    Me.emptyFn = function () {};

    /**
     * Add target property to source
     * @param source
     * @param target
     * @returns {*|{}}
     */
    Me.apply = function (source, target) {
        source = source || {};
        if (typeof target == 'object') {
            for (var name in target) {
                if (target.hasOwnProperty(name)) {
                    source[name] = target[name];
                }
            }
        }
        return source;
    }

    /**
     * Add target property to source if source has this property
     * @param source
     * @param target
     * @throw Error if the source parameter is not object.
     */
    Me.applyIf = function (source, target) {
        if (source) {
            if (typeof target == 'object') {
                for (var name in target) {
                    if (target.hasOwnProperty(name) && source.hasOwnProperty(name)) {
                        source[name] = target[name];
                    }
                }
            }
        } else {
            throw Error('The first parameter must be object.')
        }
    }
    /**
     * Add property to function
     * @param clz
     * @param target
     */
    Me.applyClass = function (clz, target) {
        if (typeof clz == 'function') {
            for (var name in target) {
                if (target.hasOwnProperty(name)) {
                    clz.prototype[name] = target[name]
                }
            }
        }
    }

    Me.namespace = function (ns) {
        if (ns == null || ns == undefined || ns == '') {
            throw Error('namespace must not be empty.');
        }
        var nsWords = ns.split('.'),
            temp;
        for (var i = 0; i < nsWords.length; i++) {
            var word = nsWords[i];
            if (i == 0) {
                global[word] = global[word] || {};
                temp = global[word];
            } else {
                temp[word] = temp[word] || {};
                temp = temp[word];
            }
        }
        return temp;
    }

    Me.ns = Me.namespace;
    
    Me.ns("Me.util");
    Me.apply(Me.util,{
        isFunction: function(fn){
            return typeof fn == 'function'
        }
    })
    
    /**
     * 基类
     * @param config
     * @constructor
     */
    Me.Class = function (config) {
        Me.apply(this, {
            className: 'Me.Class',
            extends: Object,

            getClassName: function () {
                return this.className;
            },

            getName: function () {
                return this.className.substr(this.className.lastIndexOf(".") + 1);
            }
        });

        Me.apply(this, config);
        this.extends.call(this);
        return this;
    }
    
    Me.createClass = function(className){
        
    }
    /**
     * 定义一个类
     * <p>keywords: extends,statics
     */
    Me.define = function (className, config) {
        var namespace = className.substr(0, className.lastIndexOf(".")),
            name = className.substr(className.lastIndexOf(".") + 1),
            config = config || {};
        
        if (name == namespace) {
            namespace = Me;
        }else{
            namespace = Me.ns(namespace);
        }

        if(!Me.util.isFunction(config.extends)){
            config.extends = Me.Class;
        }

        Me.applyClass(temp,{
            className: className
        });

        function temp(config){

            console.log(this.className);
            this.extends.call(this,config);
        }
        Me.applyClass(temp,config);
        namespace[name] = temp;
    }

})(global);

if (typeof module != undefined) {
    module.exports = Me;
}
